package com.sinog.auth.config;

import com.sinog.auth.feign.UserLoginCheckService;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @desc 登录成功监听器
 * @author lj
 * @date -
 */
@Component
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Resource
    private UserLoginCheckService userLoginCheckService;

    /**
     * 登录成功监听器
     * @param event event
     */
    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        Object principal = event.getAuthentication().getPrincipal();
        Object details = event.getAuthentication().getDetails();
        String ip = ((WebAuthenticationDetails)details).getRemoteAddress();
        String userAccount = ((UserDetails)principal).getUsername();
        if(null != ((WebAuthenticationDetails)details).getSessionId()) {
            userLoginCheckService.insertUserLoginDetails(userAccount,ip,"1","");
        }
    }
}